//重置样式
require('../../assets/css/index.less')

//引入swiper
require('../../assets/lib/swiper/swiper.css')
let Swiper = require('../../assets/lib/swiper/swiper.js')

//引入weui库
require('../../assets/lib/weui/weui.css')
let weui = require('../../assets/lib/weui/weui.js')
console.log(weui);

//当前页面样式
require('./home.less')

//引入字体库
require('../../assets/fonts/iconfont.css')



//引入dom
const dom = require('../../utils/dom.js')

//引入hppts
const https = require('../../utils/https.js')

//判定token是否放行
dom.getToken()

dom.reday(function () {

    dom.footerH('home')

    //swiper
    //轮播图
    new Swiper('.swiper', {
        // direction: 'vertical', // 垂直切换选项
        loop: true, // 循环模式选项

        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
        },

    })

    //点击跳转课程训练
    dom.getEvent('#goCourse',function(){
        location.href='courseTraining.html'
    })

    dom.getEvent('#goRun',function(){
        location.href='run.html'
    })



    //动态渲染数据 获取个人信息
    function getData() {
        https.get('/api/user/info', function (res) {
            let { errno, data } = res
            if (errno == 0) {
                ranking.textContent = data.ranking;
                clockCount.textContent = `累计打卡${data.clockCount}天`
                badges.textContent = data.badges
            }
        })
    }
    getData()

    //获取打卡信息
    function getClock() {
        https.get('/api/user/clockInInfo', function (res) {
            let { errno, data } = res
            if (errno == 0) {
                clockCount.textContent = `累计打卡${data.clockCount}天`
                clockBtn.textContent = data.isClockIn ? '已打卡' : '今日打卡'
            }

        })
    }
    getClock()

    //打卡
    function setClock() {

        dom.getEvent('#clockBtn', function () {
            https.post('/api/user/clockIn', {}, function (res) {
                console.log(res);
                let { errno, message } = res
                if (errno == -1) {
                    weui.topTips(message)
                }else{
                    weui.toast('打卡成功')
                    getClock()
                }
            })
        })


    }
    setClock()
})